import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login',
        component: Home
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../components/Login.vue')
    },
    {
        path: '/home',
        name: 'Home',
        component: () => import('../views/Home.vue'),
        redirect: '/index',
        children: [
            {
                path: '/index',
                name: 'Index',
                component: () => import('../views/Index.vue')
            },
            {
                path: '/user',
                name: 'User',
                component: () => import('../views/User.vue')
            },
            {
                path: '/order',
                name: 'Order',
                component: () => import('../views/Order.vue')
            },
            //课程管理
            {
                path: '/course/video',
                name: '视频详情',
                component: () => import('../views/course/Video.vue')
            },
            {
                path: '/course/info',
                name: '发布课程',
                component: () => import('../views/course/Info.vue')
            },
            {
                path: '/course/chapter/:id',
                name: '添加章',
                component: () => import('../views/course/Chapter.vue')
            },
            {
                path: '/course/episode/:id',
                name: '添加集',
                component: () => import('../views/course/Episode.vue')
            },
            {
                path: '/course/ChapterAndEpisode/:id',
                name: '章节信息',
                component: () => import('../views/course/ChapterAndEpisode.vue')
            },
            {
                path: '/course/video_banner',
                name: '轮播图管理',
                component: () => import('../views/course/Video_banner.vue')
            },

        ]
    },


]


const router = new VueRouter({
    routes
})


// 导航守卫
router.beforeEach((to, from, next) => {
    const token = window.sessionStorage.getItem('token');
    if (to.path !== '/login') {
        if (!token) {
            next('/login')
        } else {
            next()
        }
    } else {
        next()
    }

})

export default router
